<?php
namespace app\admin\controller;
use think\Controller;
class Base extends Controller
{
	public function initialize(){
		if(!session('username')){
            $this->redirect(url('login/login'));
        }
	}
	// 原图上传
    public function uploadtmd(){
        $photo='';
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        if(isset($_FILES['file'])){
            move_uploaded_file($_FILES['file']['tmp_name'],$imgpath.$imgsubpath.$_FILES['file']['name']);
            $photo="/uploads/images/".$imgsubpath.$_FILES['file']['name'];
        }
        return json(['code' => 200, 'photo' =>$photo, 'msg' => '成功']);
    }
    //压缩图片并上传
    public function uploads(){
        header("Content-type: image/jpeg");
        $files=$_FILES['file'];
        $file = $files['tmp_name']; 
        $percent = 1; //图片压缩比 
        list($width, $height) = getimagesize($file); //获取原图尺寸 
        //缩放尺寸 
        $newwidth = $width * $percent; 
        $newheight = $height * $percent; 
        if(getimagesize($file)[2]==2){
           $src_im = imagecreatefromjpeg($file);
        }elseif(getimagesize($file)[2]==3){
            $src_im = imagecreatefrompng($file);
        }
        $dst_im = imagecreatetruecolor($newwidth, $newheight); 
        imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        imagejpeg($dst_im,$imgpath.$imgsubpath.$files['name']); //输出压缩后的图片 
        imagedestroy($dst_im); 
        imagedestroy($src_im);
        return json(['code' => 200, 'data' =>"/uploads/images/".$imgsubpath.$files['name'], 'msg' => '成功']);
    }
}